package cn.seaboot.excel.sax;

import org.apache.poi.ss.usermodel.Sheet;
import org.dom4j.Attribute;
import org.dom4j.Element;

import java.io.IOException;

/**
 * parse tag: colgroup
 *
 * @author Mr.css
 * @version 2025-07-01 14:37
 */
public class TableColumnParser implements DocumentParser {

    /**
     * 宽度缩放
     * <p>
     * Excel 中的单位比较特殊（字符单位，正常要除以 256，个人推荐值为 32，这与屏幕分辨率有关）
     */
    private int widthScale = 64;

    public int getWidthScale() {
        return widthScale;
    }

    public void setWidthScale(int widthScale) {
        this.widthScale = widthScale;
    }

    /**
     * 要操作的 excel 上下文
     */
    private ExcelContext context;

    public ExcelContext getContext() {
        return context;
    }

    public void setContext(ExcelContext context) {
        this.context = context;
    }

    @Override
    public void parse(Element colgroup) throws IOException {
        Sheet sheet = context.getSheet();
        int n = 0;
        for (Element element : colgroup.elements()) {
            Attribute attr = element.attribute("width");
            if(attr != null) {
                sheet.setColumnWidth(n, Integer.parseInt(attr.getValue()) * widthScale);
            }
            n++;
        }
    }
}
